<template>
  <div id="app">
    <router-view />
  </div>
</template>
<script>
export default {
  data() {
    return {
      token: "",
      weixinCode: "",
      openId: "",
    };
  },
  mounted() {
    this.login();
  },
  methods: {
    login() {
      const AppId = "wx012abfdd79f2110c"; // 测试公众号平台的APPID，第1步那个链接里
      const code  =this.getParamName("code"); // 获取当前页面地址中的code参数的值
      const local =encodeURIComponent( window.location.href); // 对当前地址用encodeURIComponent进行编码

     // 如果code是''，说明还没有授权，访问下面连接，用户同意授权，获取code
      if (code == "") {
        window.location.href = `https://open.weixin.qq.com/connect/oauth2/authorize?appid=${AppId}
				&redirect_uri=${local}&response_type=code&scope=snsapi_base&state=STATE#wechat_redirect`;
      } else {
        this.weixinCode = code; // 能拿到code，说明用户已同意授权，拿到coed
        console.log("获取微信code：", this.weixinCode);

        this.$axios
          .post("http://a.chuxingcai.com/prod-api/webapi/wx/getOpenid?code="+this.weixinCode)
          .then((res) => {
            this.openId = res.data.data.datum;
             console.log("openid11",res)

            this.$axios
              .post("http://a.chuxingcai.com/prod-api/webapi/user/login", {
                fromSource: "h5",
                openId: this.openId
              })
              .then((res11) => {
                 console.log("openid22",res11)
                this.token = res11.data.data.token;
                console.log("token", this.token);
                window.sessionStorage.setItem("token", this.token);
              });

            // this.token=res.data.data.token
            // console.log("token",this.token);
            // window.sessionStorage.setItem('token',this.token)
          });
      }

      // this.$axios
      //   .post("http://a.chuxingcai.com/prod-api/webapi/user/login", {
      //     fromSource: "h5",
      //     openId: "12345678",
      //   })
      //   .then((res) => {
      //     this.token = res.data.data.token;
      //     console.log("token", this.token);
      //     window.sessionStorage.setItem("token", this.token);
      //   });
    },
    getParamName(name) {
      var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
      var r = window.location.search.substr(1).match(reg);
      if (r != null) {
        return unescape(r[2]);
      } else {
        return '';
      }
    },
  },
};
</script>
<style lang="scss">
#app {
  font-family: Avenir, Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  color: #2c3e50;
}

body {
  background-color: #f6f6f6;
}
* {
  margin: 0;
  padding: 0;
}
</style>
